﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using DevExpress.XtraEditors;
using ERP_RORZE_DLL.Product.DAO;
using ERP_RORZE_DLL.Product.database;
using ERP_RORZE_PERMISSION.Base;

namespace ERP_RORZE_PRODUCT.Product.WTS
{
    public partial class frm_LaborWTSEdit : DevExpress.XtraEditors.XtraForm
    {
        //Không dùng form này
        DataTable dt;
        ClsWTSDAO clsWTSDAO = new ClsWTSDAO();
        ClsLaborWTSTable b = new ClsLaborWTSTable();
        DataTable dtStaff = new DataTable();
        DataTable dtMoNo = new DataTable();
        private bool flagLoadDone;
        private bool dropDown;

        public event EventHandler Reload;

        public string ID;
        public string Date;
        public string Shift;
        public string MachineID;
        public string StaffID;
        public string MONo;
        public string OptionID;
        public string WorkID;
        public string Time;
        public string OKQty;
        public string NGQty;
        public string ProTime;
        public string ClampTime;
        public string Note1;
        public string Note2;
        private LogUtils log;

        public frm_LaborWTSEdit()
        {
            InitializeComponent();
            log = new LogUtils();
        }

        private void btnExit_Click(object sender, EventArgs e)
        {
            this.Close();
        }

        private void frm_LaborWTSEdit_Load(object sender, EventArgs e)
        {
            //list Shift
            DataTable dtShift = new DataTable();
            dtShift.Columns.Add("Display");
            dtShift.Columns.Add("Value");
            for (int i = 0; i < 4; i++)
            {
                DataRow rShift = dtShift.NewRow();
                rShift["Display"] = i.ToString();
                rShift["Value"] = "T" + i.ToString();
                dtShift.Rows.Add(rShift);
            }
            cbShift.DataSource = dtShift;
            cbShift.DisplayMember = "Display";
            cbShift.ValueMember = "Value";


            //list machine
            dt = clsWTSDAO.GetAllMachine();
            cbMachine.DataSource = dt;
            cbMachine.DisplayMember = "MachineID";
            cbMachine.ValueMember = "MachineID";

            //list MO Number
            DataTable dt2;
            dtMoNo = clsWTSDAO.GetAllLWTSMONo();
            DataRow r1 = dtMoNo.NewRow();
            r1["MONo"] = "";
            r1["FullName"] = "";
            dtMoNo.Rows.InsertAt(r1, 0);
            cbMONo.DataSource = dtMoNo;
            cbMONo.DisplayMember = "MONo";
            cbMONo.ValueMember = "MONo";



            //list staff
            dtStaff = clsWTSDAO.GetAllStaff();
            cbStaff.DataSource = dtStaff;
            cbStaff.DisplayMember = "StaffID";
            cbStaff.ValueMember = "StaffID";

            //list work
            dt = clsWTSDAO.GetAllWorkLWTS();
            cbWorkID.DataSource = dt;
            cbWorkID.DisplayMember = "WorkID";
            cbWorkID.ValueMember = "WorkID";

            dateDate.Value = DateTime.Parse(Date);
            txtClampTime.Text = ClampTime;
            txtNGQty.Text = NGQty;
            txtNote1.Text = Note1;
            txtNote2.Text = Note2;
            txtOKQty.Text = OKQty;
            txtProTime.Text = ProTime;
            txtTime.Text = Time;
            cbMachine.Text = MachineID;
            cbMONo.SelectedValue = MONo;
            cbOption.Text = OptionID;
            cbShift.Text = Shift;
            cbStaff.SelectedValue = StaffID;
            cbWorkID.SelectedValue = WorkID;

            flagLoadDone = true;
        }

        private void btnUpdate_Click(object sender, EventArgs e)
        {
            try
            {
                int.Parse(txtTime.Text);
                int.Parse(txtClampTime.Text);
                int.Parse(txtNGQty.Text);
                int.Parse(txtOKQty.Text);
                int.Parse(txtProTime.Text);
                //
                b.ID = int.Parse(ID);
                b.Clamptime = float.Parse(txtClampTime.Text);
                b.Date = dateDate.Value;
                b.MachineID = cbMachine.Text;
                b.MONo = cbMONo.SelectedValue.ToString();
                b.NGQty = int.Parse(txtNGQty.Text);
                b.Note1 = txtNote1.Text;
                b.Note2 = txtNote2.Text;
                b.OKQty = int.Parse(txtOKQty.Text);
                b.OptionID = cbOption.Text;
                b.ProTime = float.Parse(txtProTime.Text);
                b.Shift = cbShift.SelectedValue.ToString();
                b.StaffID = cbStaff.SelectedValue.ToString();
                b.Time = float.Parse(txtTime.Text);
                b.WorkID = cbWorkID.SelectedValue.ToString();
                clsWTSDAO.UpdateLaborWTS(b);

                //write Log
                log.Table = LogConstant.Table.Product.LaborWTS;
                log.Task = LogConstant.Task.Update;
                log.Description = "Date: " + b.Date + "; MONo: " + b.MONo + "; MachineID: " + b.MachineID + "; OptionID: "
                            + b.OptionID + "; Shift: " + b.Shift + "; WorkID: " + b.WorkID + "; Time: " + b.Time
                            + "; ProTime: " + b.ProTime + "; ClampTime: " + b.Clamptime;
                log.WriteLog();

                MessageBox.Show("Đã nhập dữ liệu xong");

                //cbWorkID.Text = "";
                //txtClampTime.Text = "0";
                //txtNote1.Text = "";
                //txtNGQty.Text = "0";
                //txtNote2.Text = "";
                //txtOKQty.Text = "0";
                //txtProTime.Text = "0";
                //txtTime.Text = "";

                Reload(sender, e);
            }

            catch
            {
                MessageBox.Show("Time, ClampTime, NGQty, OKQty, ProTime phải là kiểu số");
            }
        }

        private void cbMONo_SelectedValueChanged(object sender, EventArgs e)
        {
            //list  OptionID
            //dt = clsWTSDAO.GetAllOptionID(cbMONo.SelectedValue.ToString());
            //cbOption.DataSource = dt;
            //cbOption.DisplayMember = "OptionID";
            //cbOption.ValueMember = "OptionID";
        }

        private void frm_LaborWTSEdit_KeyDown(object sender, KeyEventArgs e)
        {
            if (e.Alt)
            {
                if (e.KeyCode == Keys.D)
                {
                    this.btnUpdate.PerformClick();
                }
            }
            else if (e.KeyCode == Keys.End)
            {
                btnUpdate.Focus();
            }
            else if (e.KeyCode == Keys.Home)
            {
                dateDate.Focus();
            }
        }

        protected override bool ProcessCmdKey(ref Message msg, Keys keyData)
        {
            // Add controls for which Arrow keys => focus is not required.
            if ((!dropDown && (ActiveControl is TextBox || ActiveControl is System.Windows.Forms.ComboBox || ActiveControl is System.Windows.Forms.DateTimePicker)))
            {
                switch (keyData)
                {
                    case Keys.Up:
                    case Keys.Down:

                        bool forward = (keyData == Keys.Down);
                        if (this.ProcessTabKey(forward))
                        {
                            return true;
                        }

                        break;
                }
            }
            return base.ProcessCmdKey(ref msg, keyData);
        }

        protected override bool ProcessKeyPreview(ref Message m)
        {
            if (m.Msg == 0x0100 && (int)m.WParam == 13)
            {
                this.ProcessTabKey(true);
            }
            return base.ProcessKeyPreview(ref m);
        }

        private void cbStaff_SelectedIndexChanged(object sender, EventArgs e)
        {
            if (flagLoadDone)
            {
                DataTable dt3;
                dt3 = clsWTSDAO.GetAllLOptionID(cbMONo.SelectedValue.ToString());
                DataRow r2 = dt3.NewRow();
                r2["OptionID"] = "";

                dt3.Rows.InsertAt(r2, 0);
                cbOption.DataSource = dt3;
                cbOption.DisplayMember = "OptionID";
                cbOption.ValueMember = "OptionID";

                if (dtMoNo.Rows.Count > 0)
                {
                    DataRow[] row = dtMoNo.Select("MONo ='" + cbMONo.SelectedValue.ToString() + "'");
                    lblPartNo.Text = row[0]["PartID"].ToString();
                }
            }
        }

        private void cbMONo_SelectedIndexChanged(object sender, EventArgs e)
        {
            if (flagLoadDone && dtStaff.Rows.Count > 0)
            {
                DataRow[] row = dtStaff.Select("StaffID ='" + cbStaff.SelectedValue.ToString() + "'");
                lblStaffName.Text = row[0]["StaffName"].ToString();
            }
        }

        private void txtNote2_Enter(object sender, EventArgs e)
        {
            ((TextBox)sender).SelectAll();
        }

        private void txtNote2_MouseDown(object sender, MouseEventArgs e)
        {
            ((TextBox)sender).SelectAll();
        }

        private void cbShift_DropDown(object sender, EventArgs e)
        {
            dropDown = true;
        }

        private void cbShift_DropDownClosed(object sender, EventArgs e)
        {
            dropDown = false;
        }

        private void lblStaffName_MouseDown(object sender, MouseEventArgs e)
        {
            ((TextBox)sender).SelectAll();
        }

    }
}